//
//  HZGoodsCategoryColHeadView.m
//  app
//
//  Created by 刘华舟 on 15/5/21.
//  Copyright (c) 2015年 hdaren. All rights reserved.
//

#import "HZGoodsCategoryColHeadView.h"
#import "UIImageView+URL.h"
#import "HZCollectionView.h"

#import "HZFindCategoriesColHeadModel.h"

static const NSInteger kGoodsCategoryColMargin = 24;
static const NSInteger kGoodsCategoryColLabelHeight = 60;

@interface HZGoodsCategoryColHeadView()

@property (strong, nonatomic) HZFindCategoriesColHeadModel *model;

@property (strong, nonatomic) UIImageView *imageView;
@property (strong, nonatomic) UILabel *nameLabel;

@end

@implementation HZGoodsCategoryColHeadView

+ (instancetype)cellWithCollectionView:(HZCollectionView *)collectionView dequeueReusableSupplementaryViewOfKind:(NSString*)elementKind forIndexPath:(NSIndexPath *)indexPath{
    NSString *cellReuseIdentifier = @"HZGoodsCategoryColHeadView";
    HZGoodsCategoryColHeadView * header= (HZGoodsCategoryColHeadView *)[collectionView dequeueReusableSupplementaryViewOfKind:elementKind withReuseIdentifier:cellReuseIdentifier forIndexPath:indexPath];
    if (header == nil) {
        NSLog(@"创建HZGoodsCategoryColHeadView失败");
    }
    return header;
}

- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = HZ_CLEAR_COLOR;
        
        CGFloat imageViewWidth = [self.ubo fw:UB_WIDTH uw:-2*kGoodsCategoryColMargin];
        self.imageView = [[UIImageView alloc] initWithFrame:CGRectOf([self.ubo fx:UB_LEFT ux:kGoodsCategoryColMargin fy:UB_TOP uy:kGoodsCategoryColMargin],
                                                                     [self.ubo ow:imageViewWidth oh:imageViewWidth/3])];
        self.imageView.backgroundColor = HZ_MALL_S_LIGHT_GRAY_BACKGROUND_COLOR;
        self.imageView.userInteractionEnabled = YES;
        UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imageViewTaped)];
        [self.imageView addGestureRecognizer:tapGesture];
        [self addSubview:self.imageView];
        
        self.nameLabel = [[UILabel alloc] initWithFrame:CGRectOf([self.ubo fx:UB_LEFT ux:kGoodsCategoryColMargin fy:UB_BOTTOM uy:-kGoodsCategoryColLabelHeight],
                                                                 [self.ubo fw:UB_WIDTH uw:-2*kGoodsCategoryColMargin uh:kGoodsCategoryColLabelHeight])];
        [self.nameLabel setTextColor:HZ_MALL_TEXT_S_LIGHTGRAY_COLOR];
        self.nameLabel.textAlignment = NSTextAlignmentLeft;
        [self.nameLabel setFont:kHZFont_Content_S];
        [self addSubview:self.nameLabel];
    }
    return self;
}

#pragma mark 点击图片
- (void)imageViewTaped{
    if (self.tabSelectedCaller) {
        [self.tabSelectedCaller call:self.model];
    }
}

#pragma mark 加载图片数据
-(void)showImageViewWithHeadModel:(HZFindCategoriesColHeadModel *)model{
    
    _model = model;
    
    //[self.imageView setImageWithURL:model.adPic];
    
    //图像的动画特效
    [self.imageView sd_setImageWithURL:[NSURL URLWithString:model.adPic] placeholderImage:nil options:SDWebImageRetryFailed progress:^(NSInteger receivedSize, NSInteger expectedSize) {
        
    } completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
        
        if (cacheType == SDImageCacheTypeNone) {
            self.imageView.alpha = 0.0f;
            [UIView animateWithDuration:0.5f animations:^{
                self.imageView.alpha = 1.0f;
            }];        }
        
    }];
    
    
    [self.nameLabel setText:model.title];
    
}

@end
